const { createClient } = require("redis");
const axios = require("axios");
const WinexAuthorizationGenerator = require("./WinexAuthorizationGenerator");

class WinexScheduleGenerator {

    constructor(redisHost = "127.0.0.1", redisPort = 6379, redisDb = 0) {
        this.redisHost = redisHost;
        this.redisPort = redisPort;
        this.redisDb = redisDb;
        this.authorization = null;
    }

    async generate() {
        const winexAuthorizationGenerator = new WinexAuthorizationGenerator();
        this.authorization = await winexAuthorizationGenerator.getAuthorization("hisAuthorizations");
        let url = `${this.authorization.hostname}/schedule-outpatient/api/v1/app_encounter_schedule/register_num_pool/query/by_example`;
        let data = JSON.stringify({
            "pageType": "P",
            "pageSize": 30,
            "pageNo": 0,
            "encDeptId": "",
            "essId": "",
            "noonTypeCode": "",
            "essTypeCode": "",
            "essCategoryId": "",
            "autoSelectorFlag": "98175",
            "doctorId": "",
            "optSpecialityIdList": [
                ""
            ],
            "scheduledDate": new Date().toISOString().split('T')[0],
            "seqProperty": "1",
            "seqType": "desc",
            "serviceChannelTypeCode": "253604",
            "mergeEssFlag": "98176",
            "gridName": "regiestTable",
            "bizResourceId": "",
            "hospitalAreaId": this.authorization.serviceHospitalSOID,
            "hospitalSOID": this.authorization.hospitalSOID,
            "soid": [
                this.authorization.hospitalSOID
            ]
        });
        const config = {
            method: "post",
            maxBodyLength: Infinity,
            url: url,
            headers: {
                "Content-Type": "application/json",
                "Authorization": this.authorization.authorization,
            },
            data: data,
        };
        if (config.url.startsWith("https://")) {
            config.httpsAgent = new (require("https").Agent)({ rejectUnauthorized: false });
        }
        const response = await axios.request(config);
        let schedules = response.data.data;
        if (!schedules || schedules.length === 0) {
            return;
        }
        schedules = schedules.map(schedule => {
            return {
                essName: schedule.essName,
                noonTypeName: schedule.noonTypeName,
                essId: schedule.essId,
                essTypeCode: schedule.essTypeCode,
                noonTypeCode: schedule.noonTypeCode,
                essChannelId: schedule.essChannelId,
                essCategoryId: schedule.essCategoryId,
                encChargeServiceId: schedule.encChargeServiceId,
                essCategoryCode: schedule.essCategoryCode,
                bizResourcePoolId: schedule.bizResourcePoolId,
                encDeptId: schedule.encDeptId,
            }
        })
        const client = await createClient({ host: this.redisHost, port: this.redisPort }).on("error", () => { }).connect();
        await client.select(this.redisDb);
        await client.del("schedules");
        for (let i = 0; i < schedules.length; i++) {
            await client.rPush("schedules", JSON.stringify(schedules[i]));
        }
        await client.disconnect();
    }

}

module.exports = WinexScheduleGenerator;